#include "stdafx.h"
#include "AudioManager.h"
#include "Sound3D.h"
#include "Sound2D.h"

AudioManager::AudioManager(const String& directory) : m_directory(directory){}
AudioManager::~AudioManager(void){Clear();}

Sound3D* AudioManager::Load3DSound(const String& filepath){
	auto it = m_3d_sounds.find(filepath);
	if (it == m_3d_sounds.end()){
		Sound3D* sound = new Sound3D;
		sound->Load(m_directory + filepath);
		m_3d_sounds.insert(std::make_pair(filepath, sound));
		return sound;
	}
	return it->second;
}

Sound2D* AudioManager::Load2DSound(const String& filepath){
	auto it = m_2d_sounds.find(filepath);
	if (it == m_2d_sounds.end()){
		Sound2D* sound = new Sound2D;
		sound->Load(m_directory + filepath);
		m_2d_sounds.insert(std::make_pair(filepath, sound));
		return sound;
	}
	return it->second;
}

inline void AudioManager::Clear(){
	if (!m_2d_sounds.empty()){
		for (auto it = m_2d_sounds.begin(); it != m_2d_sounds.end(); it++){
			delete it->second;
			it->second = nullptr;
		}
		m_2d_sounds.clear();
	}
	if (!m_3d_sounds.empty()){
		for (auto it = m_3d_sounds.begin(); it != m_3d_sounds.end(); it++){
			delete it->second;
			it->second = nullptr;
		}
		m_3d_sounds.clear();
	}
}

void AudioManager::Remove2DSound(const String& filename){
	auto it = m_2d_sounds.find(filename);
	if (it != m_2d_sounds.end()){
		delete it->second;
		it->second = nullptr;
		m_2d_sounds.erase(it);
	}
}

void AudioManager::Remove3DSound(const String& filename){
	auto it = m_3d_sounds.find(filename);
	if (it != m_3d_sounds.end()){
		delete it->second;
		it->second = nullptr;
		m_3d_sounds.erase(it);
	}
}